 DO  GetCTPHeaderINI ! 240910   
  ! [Priority 5000]
  DO LOAD_FieldMappings !240922
 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

GetCTPHeaderINI ROUTINE ! (PHD) Creation !240910    
    GLocation =  GETINI('Header','Location',,'.\CTPHeader.INI') 
    GTemplateNumber = GETINI('Header','TemplateNumber',,'.\CTPHeader.INI')
    GUser = GETINI('Header','User',,'.\CTPHeader.INI')
    GDBTableName = GETINI('Header','DBTableName',,'.\CTPHeader.INI')
    GEncrypt = GETINI('Header','GEncrypt',,'.\CTPHeader.INI')
    GUniqueSourceColumn = GETINI('Header','UniqueSourceColumn',,'.\CTPHeader.INI')
   
    ! Update SQL Table etpprohd !240922
    CLEAR(etpprohd)
!    Access:etpprohd.PrimeRecord()
    ETPHD:OID = 1
    ETPHD:TemplateNumber = GTemplateNumber
    ETPHD:UserName = GUser
    ETPHD:DBTableName = GDBTableName
    ETPHD:Location = GLocation
    ETPHD:Gencrypt = GEncrypt
    ETPHD:UniqueSourceColumn = GUniqueSourceColumn
    IF Access:etpprohd.Insert() ; STOP(error() & ' @ Access:etpprohd.Insert ').
    

! [Priority 500]
LOAD_FieldMappings  ROUTINE
    IF ~FieldMappings
        FieldMappings = GETINI('FieldMappings','FieldMappings',,'.\FieldMappings.ini')
    END
!   STOP('FieldMappings = ' & clip(FieldMappings)) !ok
    IF FieldMappings 
        DO ParseFieldMappings       ! Create and fill FieldMappingQueue !240922 Upd
    END

! [Priority 500]
ParseFieldMappings  ROUTINE !(FieldMappings) < (STRING pFieldMappings) !240825
    
! FieldMappings Set before calling this to put them into a queue as Key,value pair: Dx=FieldName

 ! ParseFieldMappings   
 
 ! Example Call:
 ! sFields STRING(256) 
 !   FieldMappings = 'D1=birthdate,D2=SocSecNo,D3=Telephone,D4=email'
 !   DO ParseFieldMappings
 ! Example to display the parsed results
 !  DISPLAYParsedFieldMappings()    
 ! Flag when FieldMap is used
 ! Use Que when FieldMap has values    
 ! GBL:DataFieldName = FMQ:FieldName
 ! When looking through BData in order of Gstlabls by GBL:KeyTemplateFieldNo
 ! Now at the Procedure local level: 
 ! FieldMappingQueue       QUEUE,PRE(FMQ)
 ! FieldName               STRING(20) ! eg. D1
 ! FieldValue              STRING(40) ! eg. Birthdate
 !                    END    

DATA

locFieldMappings    STRING(64000) 
locPair     STRING(256)
locKey      STRING(256)
locValue    STRING(256)        
pos LONG
posEqual    LONG
nextComma   LONG
      
CODE 
    locFieldMappings = FieldMappings 
    pos = 1
  ! Main code block
    LOOP
        nextComma = INSTRING(',', locFieldMappings,, pos)  ! Find the next comma
        IF nextComma = 0  
            nextComma = LEN(CLIP(locFieldMappings)) + 1  ! Handle the last item if no comma is found
        END
        locPair = SUB(locFieldMappings, pos, nextComma - pos)  ! Get the pair
        locKey = SUB(locPair, 1, INSTRING('=', locPair) - 1)  ! Extract key
        locValue = SUB(locPair, INSTRING('=', locPair) + 1, LEN(CLIP(locPair)) - INSTRING('=', locPair))        
 
!      ! add to the queue
!        CLEAR(FieldMappingQueue)
!        FMQ:FieldName = locKey
!        FMQ:FieldValue = locValue
!        ADD(FieldMappingQueue)          
!        
        ! add to the FieldMappings Table etpprofm !240922
        CLEAR(etpprofm) 
!        Access:etpprofm.PrimeRecord() 
        ETPFM:TemplateNumber = ETPHD:TemplateNumber 
        !SUB(string,position,length)
        Len# = LEN(CLIP(locKey))
        ETPFM:DataFieldNumber = SUB(locKey,2,Len#-1) ! Parse Dx number to save
        ETPFM:DataFieldName = locValue ! eg. birthdate
        
!   STOP('ETPFM:TemplateNumber = ' & ETPFM:TemplateNumber &' ETPFM:DataFieldNumber = ' & ETPFM:DataFieldNumber & ' ETPFM:DataFieldName = ' & ETPFM:DataFieldName )
        If Access:etpprofm.Insert()
             STOP('error : ETPFM:TemplateNumber = ' & ETPFM:TemplateNumber &'ETPFM:DataFieldNumber = ' & ETPFM:DataFieldNumber & ' ETPFM:DataFieldName = ' & ETPFM:DataFieldName )
        END
        
        
!       STOP('Field: ' & FMQ:FieldName & ' | Value: ' & FMQ:FieldValue)
        ! Update position to start after the comma
        pos = nextComma + 1  ! Update position to start after the comma
        IF pos > LEN(CLIP(locFieldMappings)) THEN BREAK.  ! Exit if end of string is reached
    END 
    
 